package com.hd.binlog.thread;

import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.hd.binlog.config.MySqlHost;
import com.hd.binlog.core.BinlogDataDispatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author zhanghaidong
 * @version v1.0
 * @description
 * @date 2019/9/24
 */
public class BinLogListenerThread implements Runnable {
    private MySqlHost host;
    private BinlogDataDispatcher listener;

    private Logger logger = LoggerFactory.getLogger(BinLogListenerThread.class);

    public BinLogListenerThread(MySqlHost host, BinlogDataDispatcher listener) {
        this.host = host;
        this.listener = listener;
    }

    @Override
    public void run() {
        BinaryLogClient client = new BinaryLogClient(host.getHost(), host.getPort(), host.getUsername(), host.getPassword());

        client.registerEventListener(listener);

        while (true) {
            try {
                client.connect();
            } catch (IOException e) {
                logger.error("{}:{}监听器错误", host.getHost(), host.getPort(), e);
            }
        }
    }
}
